package cn.glutnn.service.impl;

import cn.glutnn.mapper.QuestionMapper;
import cn.glutnn.service.QuestionService;
import cn.glutnn.utils.ThreadLocalUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import cn.glutnn.entity.Question;

import java.util.List;
import java.util.Map;

/**
 * (Question)表服务实现类
 *
 * @author msh
 * @since 2024-10-25 09:36:08
 */
@Service
public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question> implements QuestionService {
    //分页查询所有问答
    @Override
    public Page<Question> findAllQues(Integer page, Integer limit) {
        // 创建分页对象
        Page<Question> pages = new Page<>(page, limit);
        // 执行分页查询
        Page<Question> result = this.page(pages);
        return result;
    }
    //条件模糊查询问答
    @Override
    public Page<Question> findPageQues(Integer page, Integer limit, String key) {
        // 创建分页对象
        Page<Question> pages = new Page<>(page, limit);
        // 创建QueryWrapper对象，构建模糊查询条件
        QueryWrapper<Question> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("plant_name", key);//根据关键词搜索
        // 执行分页查询
        return page(pages, queryWrapper);
    }

    @Override
    public List<Question> findAllQuesByKind(String kind) {
        // 创建QueryWrapper对象，构建模糊查询条件
        QueryWrapper<Question> queryWrapper = new QueryWrapper<>();
        Map<String,Object> threadLocal = ThreadLocalUtil.get();
        String username = (String) threadLocal.get("username");
        if ("questioner".equals(kind)){
            queryWrapper.eq("questioner",username);
        }else{
            queryWrapper.eq("expert_name",username);
        }
        return list(queryWrapper);
    }
    //根据ID查询，查询问答SelectById

}

